package cn.bulibuqi.service;

import cn.bulibuqi.bean.Employee;
import cn.bulibuqi.bean.EmployeeExample;
import cn.bulibuqi.dao.EmployeeMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @author bulibuqi
 * @create 2021-11-16:52
 */
@Service
public class EmployeeService {
//    自动注入
    @Autowired
    EmployeeMapper employeeMapper;
    /**
     * 功能描述: <br>
     * 〈查询所有员工〉
     * @Param: []
     * @Return: java.util.List<cn.bulibuqi.bean.Employee>
     * @Author: bulibuqi
     * @Date: 2021/11/27 16:56
     */

    public List<Employee> getAll() {

        return employeeMapper.selectByExampleWithDept(null);
    }

    /**
     * 功能描述: <br>
     * 〈员工保存〉
     * @Param: [employee]
     * @Return: void
     * @Author: bulibuqi
     * @Date: 2021/11/28 14:48
     */
    public void saveEmp(Employee employee) {

        employeeMapper.insertSelective(employee);
    }

    /**
     * 功能描述: <br>
     * 〈用户名是否可用〉
     * @Param: [empName]
     * @Return: boolean true 可用
     * @Author: bulibuqi
     * @Date: 2021/11/28 16:53
     */
    public boolean checkUser(String empName) {
        EmployeeExample example = new EmployeeExample();
        EmployeeExample.Criteria criteria = example.createCriteria();
        criteria.andEmpNameEqualTo(empName);
        long count = employeeMapper.countByExample(example);
        return count==0;
    }

    public Employee getEmp(Integer id) {
        Employee employee = employeeMapper.selectByPrimaryKey(id);
        return employee;
    }

    public void updateEmp(Employee employee) {
        employeeMapper.updateByPrimaryKeySelective(employee);
    }

    public void deleteEmp(Integer id) {
        employeeMapper.deleteByPrimaryKey(id);
    }

    public void deleteBatch(List<Integer> ids) {
        EmployeeExample example = new EmployeeExample();
        EmployeeExample.Criteria criteria = example.createCriteria();
        //delete from xxx where emp_id in(1,2,3)
        criteria.andEmpIdIn(ids);
        employeeMapper.deleteByExample(example);
    }
}
